package team.shixi.houtai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.shixi.houtai.common.Result;
import team.shixi.houtai.common.ResultUtil;
import team.shixi.houtai.entity.Goods;
import team.shixi.houtai.entity.Order;
import team.shixi.houtai.mapper.OrderMapper;
import team.shixi.houtai.service.OrderService;

/**
 * @author Zhangrt
 * @Description
 * @create 2023-01-10 17:06
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderMapper orderMapper;


    @DeleteMapping("/{id}")//删除数据
    public Result<?> delete(@PathVariable Long id){
        orderService.removeById(id);
        return Result.success();
    }

    @PutMapping("/{id}")//上/下商品
    public Result<?> pickgoods(@PathVariable Long id){
        Order order = orderService.getById(id);

        if (order.getStatus() == 0){
            //未取货
            order.setStatus(1);//取货
        }
        orderMapper.updateById(order);
        return Result.success();
    }

    @GetMapping//查询数据
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){

        Page<Order> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like((StringUtils.isNotBlank(search)),Order::getUsername,search)
                .or().like((StringUtils.isNotBlank(search)),Order::getName,search);

        Page<Order> ordersPage = orderMapper.selectAllOrder(page, queryWrapper);
        return ResultUtil.success(ordersPage);
    }
}
